package com.eye.catcher.controller;


import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TSysDaRole;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITSysDaRoleModuleService;
import com.eye.catcher.service.generate.ITSysDaRoleService;
import com.eye.catcher.service.generate.ITSysDaUserRoleService;
import com.eye.catcher.system.role.param.RoleDto;
import com.eye.catcher.system.role.param.RolePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
@Slf4j
@Controller
@RequestMapping("/eyeCatcher-system/sys-role")
public class TSysDaRoleController extends TSysDaBaseController {
    private final static String MODULE_CODE = "/sys-role";
    private final static String MODULE_NAME = "角色";

    @Autowired
    private ITSysDaRoleService roleService;
    @Autowired
    private ITSysDaUserRoleService userRoleService;
    @Autowired
    private ITSysDaRoleModuleService roleModuleService;

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    @ResponseBody
    public ResponseData addRole(@RequestBody TSysDaRole role) {
        String methodCode = "/addRole";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,role.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleService.addRole(role);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param role
     * @return
     */
    @PostMapping("/editRole")
    @ResponseBody
    public ResponseData editRole(@RequestBody TSysDaRole role) {
        String methodCode = "/editRole";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,role.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleService.editRole(role);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/updateStausByIds")
    @ResponseBody
    public ResponseData updateStausByIds(@RequestParam(value = "ids") String ids) {
        String methodCode = "/updateStausByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,"ids:"+ids,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleService.updateStausByIds(ids);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param roleIds
     * @return
     */
    @PostMapping("/deleteRoleByIds")
    @ResponseBody
    public ResponseData deleteRoleByIds(@RequestParam(value = "roleIds") String roleIds) {
        String methodCode = "/deleteRoleByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,"ids："+roleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.roleService.deleteRoleByIds(roleIds);
            this.userRoleService.deleteUserRoleByRoleIds(null,roleIds);
            this.roleModuleService.deleteRoleModuleByRoleIds(roleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param role
     * @return
     */
    @PostMapping("/getRoleList")
    @ResponseBody
    public ResponseData getRoleList(@RequestBody RoleDto role) {
        String methodCode = "/getRoleList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,role.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.roleService.getPageRoleList(
                            RolePage.builder()
                                    .role(role)
                                    .page(this.getPageInfo(role))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

}
